home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7022 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  57 lines

  1. Path: dawn.mmm.com!news
  2. From: kjhopps@mmm.com (Kevin J Hopps)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: meaning of int a::*b
  5. Date: 20 Feb 1996 16:57:47 GMT
  6. Organization: 3M - St. Paul, MN  55144-1000 US
  7. Message-ID: <4gcuib$507@dawn.mmm.com>
  8. References: <31284007.7977@mercury.co.il>
  9. Reply-To: kjhopps@mmm.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Shlomo Wygodny (wygodny@mercury.co.il) wrote:
  13. > In the following (compilable) program:
  14.  
  15. > class a{};
  16. > int a::*b;
  17. > void main(){}
  18.  
  19. > Does someone know what is the meaning of int a::*b; ?
  20. > It's probably a declaration of a variable b of type int a::* . 
  21. > But what
  22. > does it mean? What can be assigned to it?
  23.  
  24. The variable b is a pointer to an int member of class a.  It
  25. might help to think of b as an offset rather than a pointer.
  26. Given an object of class a, and the offset b, you can find the
  27. int member within the a that b refers to.
  28.  
  29. For example:
  30.     #include <iostream.h>
  31.     class a
  32.     {
  33.     public:
  34.     int i1;
  35.     int i2;
  36.     };
  37.     int a::*b;
  38.     int main()
  39.     {
  40.     a x;
  41.     x.i1 = 1;
  42.     x.i2 = 2;
  43.     b = &a::i1;        // "offset" of i1 in an "a."
  44.     cout << x.*b << '\n';    // prints 1
  45.     b = &a::i2;        // "offset" of i2 in an "a."
  46.     cout << x.*b << '\n';    // prints 2
  47.     return 0;
  48.     }
  49. --
  50. Kevin J. Hopps                  e-mail: kjhopps@mmm.com
  51. 3M Company                      phone:  (612) 737-4643
  52. 3M Center, Bldg. 235-2D-57      fax:    (612) 737-2700
  53. St. Paul, MN 55144-1000         Opinions are my own.  I don't speak for 3M.
  54.     But 3M speaks for me -- I did not write the following line:
  55.  
  56. Opinions expressed herein are my own and may not represent those of 3M.
  57.